<template>
	<el-dialog :title="title" v-model="drawer" width="700px" :append-to-body="true" @close="close">
		<el-form :model="form" label-width="120px" size="small">
			<el-form-item label="卡密类型">
				<el-radio-group v-model="form.virtualType" @change="selectVirtualType">
					<el-radio label="0">固定卡密</el-radio>
					<el-radio label="1">一次性卡密</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="卡密信息" v-if="form.virtualType == 0">
				<el-input v-model="form.diskInfo" type="textarea" />
			</el-form-item>
			<template v-if="form.virtualType == 1">
				<template v-for=" (item,index) in form.virtuals">
					<el-form-item :label="'卡密信息'+(index+1)">
						<div>
							<el-input v-model="item.cardNo" placeholder="卡号(非必填)" />
						</div>
						<div style="margin-left: 50px;">
							<el-input v-model="item.cardPwd" placeholder="卡密" />
						</div>
						<div style="margin-left: 20px;">
							<el-button type="text" @click="removeVirtuals(index)">删除</el-button>
							</div>
					</el-form-item>
				</template>
				<el-form-item >
				<el-button type="primary" @click="addVirtuals">+ 新增</el-button>
				</el-form-item>
			</template>
			<el-form-item>
				<el-button type="primary" @click="submitForm">确定</el-button>
				<el-button @click="close">取消</el-button>
			</el-form-item>
		</el-form>
	</el-dialog>
</template>

<script>
	import {
		computed,
		reactive,
		toRefs,
		ref
	} from 'vue'
	import {
		ElMessage
	} from 'element-plus'
	export default {
		name: 'MemberLabelDetail',
		props: {
			dialogDrawer: {
				type: Boolean,
				default: false
			},
			defaultForm: {
				type: Object,
				default () {
					return {}
				}
			}
		},
		setup(props, context) {
			const {
				dialogDrawer,
				defaultForm
			} = toRefs(props)
			const drawer = computed({
				get: () => dialogDrawer.value,
				set: val => {
					context.emit('update:dialogDrawer', val)
				}
			})
			let form = computed({
				get: () => defaultForm.value,
				set: val => {
					context.emit('update:defaultForm', val)
				}
			})
			const state = reactive({
				virtualLabel:'卡密信息',
				virtualsLength: 1
			})
			const close = () => {
				context.emit('update:dialogDrawer', false)
			}
			const submitForm = async (formEl) => {
				context.emit('onDetailSubmit', form.value)
				
			}
			const resetForm = (formEl) => {
				if (!formEl) return
				formEl.resetFields()
			}
			const selectVirtualType = (val) => {
				if(val == 1 && form.value.virtuals.length == 0){
					form.value.virtuals.push({cardNo:'',cardPwd:''})
					state.virtualLabel = state.virtualLabel + 1
				}
			}
			const addVirtuals = (val) => {
				state.virtualsLength = parseInt(state.virtualsLength) + 1
				state.virtualLabel = state.virtualLabel + state.virtualsLength
				form.value.virtuals.push({cardNo:'',cardPwd:''})
			}
			const removeVirtuals = (index) => {
				form.value.virtuals.splice(index, 1)
			}
			
			return {
				form,
				drawer,
				submitForm,
				resetForm,
				selectVirtualType,
				addVirtuals,
				removeVirtuals,
				close,
				...toRefs(state)
			}
		}

	}
</script>

<style>
</style>
